package com.example.weather.room

import android.annotation.SuppressLint
import com.example.library_base.util.LogUtil
import com.example.library_room.dao.UserDao
import com.example.library_room.db.UserDB
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

@ViewModelScoped
class UserLocalRepository @Inject constructor(){

    companion object{
        const val TAG: String = "UserLocalRepository"
    }

    @Inject
    lateinit var userDao: UserDao

    @SuppressLint("CheckResult")
    suspend fun insertUserDB(user: UserDB) = withContext(Dispatchers.IO){
        var insertResult: String? = null
        try {
            val result = userDao.insertUser(user)
            result.subscribe({
                insertResult = it.toString()
            }, {
                insertResult = it.message
            })
            insertResult
        }catch (e: Exception){
            e.message?.let { LogUtil.debug(TAG, it) }
            insertResult = e.message
        }
    }
}